﻿function setHidden(id, value, form) {
	var obj = document.getElementById(id);
	if (obj != null) {
		obj.value = value;
	} else {
		addHidden(id, value, form);
	}
}

function addHidden(id, value, form) {
	var obj = document.createElement("input")
	obj.type = "hidden";
	obj.id = id;
	obj.name = id;
	obj.value = value;
	if (!form) form = document.forms[0];
	form.appendChild(obj);
}

function doPost(url, values, method) {
	doAction(url, values, "POST");
}
function doAction(url, values, method) {
	var frm = document.createElement("form");
	frm.action = url;
	frm.method = method;
	for (var v in values)
		addHidden(v, values[v], frm)
	
	document.body.appendChild(frm);
	frm.submit();
}


String.prototype.truncateAt = function(n, c) {
    if (this.length <= n) { return this; }
    return this.substr(0, n - 1) + (c != null ? c : '...');
}
String.prototype.trim = function() {
    return this.replace(/^\s*/g, "").replace(/(?:\s*)$/g, "");
}

var createNode = function(type, attr, win) {
    var w = win || window, d = w.document, n = d.createElement(type);
    for (var i in attr) {
        if (attr[i]) {
            n.setAttribute(i, attr[i]);
        }
    }
    return n;
};

var addStyle = function(url, win, charset) {
    var w = win || window, d = w.document;
    var c = charset || "utf-8";
    var link = createNode("link", {
        "type": "text/css",
        "charset": c,
        "rel": "stylesheet",
        "href": url
    }, w);

    d.getElementsByTagName("head")[0].appendChild(link);
};
 